<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%;"
  >
    <el-col :span="20">
      <span>{{ item.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ item.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <!-- 注意:组件内标签的本质就是数据的差异，找到数据的不同，配合v-if做条件指令 -->
            <el-dropdown-menu v-if="item.manager" slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
            <el-dropdown-menu v-else slot="dropdown">
              <el-dropdown-item>添加子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
export default {
  name: 'TreeTool',
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    // 父传子的部门对象
    item: {
      type: Object,
      required: true
    }
  },
  methods: {
    // 控制下拉菜单的点击
    handleCommand(command) {
      if (command === 'del') {
        // 通知父组件，并携带部门id
        this.$emit('del-dept', this.item.id)
      }
      if (command === 'add') {
        // 通知父组件
        this.$emit('show-dept', this.item)
      }
      if (command === 'edit') {
        // 通知父组件
        this.$emit('edit-dept', this.item)
      }
    }
  }

}
</script>
